package com.like.bluetooth.executor

import android.content.Context
import com.like.bluetooth.util.BluetoothBroadcastReceiverManager

/**
 * 蓝牙命令真正的执行者。
 */
abstract class BluetoothExecutor(context: Context) {
    protected val mContext: Context = context.applicationContext
    private val bluetoothBroadcastReceiverManager by lazy {
        BluetoothBroadcastReceiverManager(mContext, onBleOn = ::onBleOn, onBleOff = ::onBleOff)
    }
    var onBleOn: (() -> Unit)? = null
    var onBleOff: (() -> Unit)? = null

    init {
        bluetoothBroadcastReceiverManager.register()
    }

    /**
     * 释放资源，并取消蓝牙开关广播监听
     */
    open fun close() {
        bluetoothBroadcastReceiverManager.unregister()
    }

    protected open fun onBleOn() {
        onBleOn?.invoke()
    }

    protected open fun onBleOff() {
        onBleOff?.invoke()
    }

}
